package com.eco.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Author : xulinyu
 * @Date: 2024/6/24 13:31
 * @Description:
 */
@Slf4j
public class FilePathUtil {


    /**
     * @param preFilePath "E:\\qianwei_project\\fishcount_in\\images\\20240318"
     * @param patternPath E:\\qianwei_project\\fishcount_in\\images\\20240318\\[^\\]+\\[^\\]+165541\.jpg
     * @return
     */
    public static String getFilePath(String preFilePath, String patternPath) {
        List<String> filePaths = new ArrayList<>();
        try (Stream<Path> paths = Files.walk(Paths.get(preFilePath))) {
            filePaths = paths
                    .filter(Files::isRegularFile)
                    .map(Path::toString)
                    .collect(Collectors.toList());
            Pattern pattern = Pattern.compile(patternPath);
            for (int i = 0; i < filePaths.size(); i++) {
                String path = filePaths.get(i);
                Matcher matcher = pattern.matcher(path);
                if (matcher.matches()) {
                    return path;
                }
            }

        } catch (IOException e) {
            log.error("获取文件夹内容失败：" + e.getMessage());
        }
        return "";
    }


}
